package com.by.controller;

import com.by.bean.PageBean;
import com.by.bean.ProductCategory;
import com.by.bean.ResultBean;
import com.by.group.ISave;
import com.by.group.V;
import com.by.query.ProductCategoryQuery;
import com.by.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * date 2025/2/25 14:40
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@RestController
@Api(tags = "产品分类")
@RequestMapping("/api")
public class ProductCategoryController extends BaseController{
    @Autowired
    ProductCategoryService productCategoryService;

    /**
     * 产品分类查询
     * @param productCategoryQuery
     * @return
     */
    @ApiOperation("产品分类查询")
    @PostMapping("/category/query")
    public ResultBean queryAll(@RequestBody ProductCategoryQuery productCategoryQuery){
        PageBean pageBean = productCategoryService.queryAll(productCategoryQuery);
        return success("查询成功",pageBean);
    }

    /**
     * 产品分类新增
     * @param productCategory
     * @param bindingResult
     * @return
     */
    @ApiOperation("产品分类新增")
    @PostMapping("/category/save")
    public ResultBean save(@Validated(ISave.class) @RequestBody ProductCategory productCategory, BindingResult bindingResult){
        V.validate(bindingResult);
        int rs = productCategoryService.save(productCategory);
        return saveResult(rs);
    }

    /**
     *  产品分类修改
     * @param productCategory
     * @return
     */
    @ApiOperation("产品分类修改")
    @PostMapping("/category/update")
    public ResultBean update(@RequestBody ProductCategory productCategory){
        int rs = productCategoryService.update(productCategory);
        return updateResult(rs);
    }

    /**
     * 产品分类删除
     * @param id
     * @return
     */
    @ApiOperation("产品分类删除")
    @DeleteMapping("/category/delete")
    public ResultBean  delete(@RequestParam int id){
        int code = id;
        int rs = productCategoryService.delete(code);
        return deleteResult(rs);
    }

    /**
     *  查询最大的code
     * @param pid
     * @return
     */
    @ApiOperation("查询最大的code")
    @GetMapping("/category/queryMaxCode")
    public ResultBean queryMaxCode(@RequestParam int pid){
        int rs = productCategoryService.queryMaxCode(pid);
        return success("查询成功",rs);
    }

}
